/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iodegree.lib.weight;

import iodegree.lib.MyTableModel;
import iodegree.lib.CommonsLib;
import iodegree.lib.D;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author Libra
 */
public class ClusterWeightUI extends javax.swing.JFrame {

    /**
     * Creates new form ClusterUI
     */
    public ClusterWeightUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DataFileChooser = new javax.swing.JFileChooser();
        gluGABAGroup = new javax.swing.ButtonGroup();
        shuffledGrp = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblOpenDatabase = new javax.swing.JLabel();
        btnQryTriple = new javax.swing.JButton();
        btnQryQuad = new javax.swing.JButton();
        btnOpenDbDefault = new javax.swing.JButton();
        btnOpenDatabase = new javax.swing.JButton();
        rdoBoth = new javax.swing.JRadioButton();
        rdoGABA = new javax.swing.JRadioButton();
        rdoGlu = new javax.swing.JRadioButton();
        chkShuffle = new javax.swing.JCheckBox();
        rdoShfGlu = new javax.swing.JRadioButton();
        rdoShfGABA = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        weightPane = new javax.swing.JScrollPane();
        weightTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblOpenDatabase.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblOpenDatabase.setText("Open Database File :");

        btnQryTriple.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnQryTriple.setText("Query Triple Weight");
        btnQryTriple.setEnabled(false);
        btnQryTriple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQryTripleActionPerformed(evt);
            }
        });

        btnQryQuad.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnQryQuad.setText("Query Quad Weight");
        btnQryQuad.setEnabled(false);
        btnQryQuad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQryQuadActionPerformed(evt);
            }
        });

        btnOpenDbDefault.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDbDefault.setText("Default");
        btnOpenDbDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDbDefaultActionPerformed(evt);
            }
        });

        btnOpenDatabase.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDatabase.setText("Open");
        btnOpenDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDatabaseActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoBoth);
        rdoBoth.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoBoth.setSelected(true);
        rdoBoth.setText("Both");
        rdoBoth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoBothActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGABA);
        rdoGABA.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGABA.setText("GABA");
        rdoGABA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGABAActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGlu);
        rdoGlu.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGlu.setText("Glu");
        rdoGlu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGluActionPerformed(evt);
            }
        });

        chkShuffle.setText("Shuffle");

        shuffledGrp.add(rdoShfGlu);
        rdoShfGlu.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoShfGlu.setSelected(true);
        rdoShfGlu.setText("Glu");
        rdoShfGlu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoShfGluActionPerformed(evt);
            }
        });

        shuffledGrp.add(rdoShfGABA);
        rdoShfGABA.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoShfGABA.setText("GABA");
        rdoShfGABA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoShfGABAActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblOpenDatabase)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(chkShuffle)
                        .addGap(18, 18, 18)
                        .addComponent(rdoShfGlu)
                        .addGap(18, 18, 18)
                        .addComponent(rdoShfGABA))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnQryTriple)
                        .addGap(18, 18, 18)
                        .addComponent(btnQryQuad))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnOpenDatabase)
                        .addGap(18, 18, 18)
                        .addComponent(btnOpenDbDefault)
                        .addGap(18, 18, 18)
                        .addComponent(rdoBoth)
                        .addGap(18, 18, 18)
                        .addComponent(rdoGlu)
                        .addGap(18, 18, 18)
                        .addComponent(rdoGABA)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblOpenDatabase)
                    .addComponent(rdoBoth)
                    .addComponent(btnOpenDatabase)
                    .addComponent(btnOpenDbDefault)
                    .addComponent(rdoGlu)
                    .addComponent(rdoGABA))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnQryTriple)
                    .addComponent(btnQryQuad))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkShuffle)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(rdoShfGlu)
                        .addComponent(rdoShfGABA)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        weightTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        weightPane.setViewportView(weightTable);

        jLabel1.setText("Synaptic Weight Distribution");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(weightPane, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(weightPane, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOpenDbDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDbDefaultActionPerformed
        pathToFile = CommonsLib.getDefaultFile();
        File f = new File(pathToFile);
        if (f.exists()) {
//            System.out.println("File " + pathToFile + " opened.");
            btnQryTriple.setEnabled(true);
            btnQryQuad.setEnabled(true);
        }
    }//GEN-LAST:event_btnOpenDbDefaultActionPerformed

    private void btnOpenDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDatabaseActionPerformed
        int returnVal = DataFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            pathToFile = DataFileChooser.getSelectedFile().getAbsolutePath();
//            System.out.println("File " + pathToFile + " opened.");
            btnQryTriple.setEnabled(true);
            btnQryQuad.setEnabled(true);
        }
    }//GEN-LAST:event_btnOpenDatabaseActionPerformed

    private void btnQryTripleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQryTripleActionPerformed
        getTriWeight();
    }//GEN-LAST:event_btnQryTripleActionPerformed

    private void rdoBothActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoBothActionPerformed
        type = 0;

    }//GEN-LAST:event_rdoBothActionPerformed

    private void rdoGluActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGluActionPerformed
        type = 1;
    }//GEN-LAST:event_rdoGluActionPerformed

    private void rdoGABAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGABAActionPerformed
        type = 2;
    }//GEN-LAST:event_rdoGABAActionPerformed

    private void btnQryQuadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQryQuadActionPerformed
        getQuadWeight();
    }//GEN-LAST:event_btnQryQuadActionPerformed

    private void rdoShfGluActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoShfGluActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdoShfGluActionPerformed

    private void rdoShfGABAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoShfGABAActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdoShfGABAActionPerformed

    private void getTriWeight() {
        if (chkShuffle.isSelected()) {
            if (rdoShfGlu.isSelected()) {
                for (int i = 0; i < 1000; i++) {
                    weight = new Weight(pathToFile).clusterWeight(type, true);
                    for (int j = 0; j < weight.length; j++) {
                        D.tpi(weight[j].getGluMean());
                    }
                    D.tp();
                }
            } else {
                for (int i = 0; i < 1000; i++) {
                    weight = new Weight(pathToFile).clusterWeight(type, true);
                    for (int j = 0; j < weight.length; j++) {
                        D.tpi(weight[j].getGabaMean());
                    }
                    D.tp();
                }
            }

        } else {
            weight = new Weight(pathToFile).clusterWeight(type, false);
            fillTable();
        }
    }

    private void getQuadWeight() {
        if (chkShuffle.isSelected()) {
            if (rdoShfGlu.isSelected()) {
                for (int i = 0; i < 1000; i++) {
                    weight = new Weight(pathToFile).clusterWeightQuad(type, true);
                    for (int j = 0; j < weight.length; j++) {
                        D.tpi(weight[j].getGluMean());
                    }
                    D.tp();
                }
            } else {
                for (int i = 0; i < 1000; i++) {
                    weight = new Weight(pathToFile).clusterWeightQuad(type, true);
                    for (int j = 0; j < weight.length; j++) {
                        D.tpi(weight[j].getGabaMean());
                    }
                    D.tp();
                }
            }

        } else {
            weight = new Weight(pathToFile).clusterWeightQuad(type, false);
            fillTable();
        }
    }

    private void fillTable() {
        Object[][] rtData = new Object[8][weight.length + 1];
        for (int i = 0; i < weight.length; i++) {
            rtData[0][i + 1] = new Integer(i);
            rtData[1][i + 1] = new Integer(weight[i].getHistoCount());
            rtData[2][i + 1] = new Double(weight[i].getGluMean());
            rtData[3][i + 1] = new Double(weight[i].getGluSD());
            rtData[4][i + 1] = new Double(weight[i].getGluSEM());
            rtData[5][i + 1] = new Double(weight[i].getGabaMean());
            rtData[6][i + 1] = new Double(weight[i].getGabaSD());
            rtData[7][i + 1] = new Double(weight[i].getGABASEM());
        }
        rtData[0][0] = "";
        rtData[1][0] = "Observed";
        rtData[2][0] = "GluMean";
        rtData[3][0] = "GluSD";
        rtData[4][0] = "GluSEM";
        rtData[5][0] = "GabaMean";
        rtData[6][0] = "GabaSD";
        rtData[7][0] = "GabaSEM";

        MyTableModel rtoTableM = new MyTableModel(rtData);
        weightPane.setColumnHeaderView(null);
        weightTable.setModel(rtoTableM);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClusterWeightUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ClusterWeightUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser DataFileChooser;
    private javax.swing.JButton btnOpenDatabase;
    private javax.swing.JButton btnOpenDbDefault;
    private javax.swing.JButton btnQryQuad;
    private javax.swing.JButton btnQryTriple;
    private javax.swing.JCheckBox chkShuffle;
    private javax.swing.ButtonGroup gluGABAGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblOpenDatabase;
    private javax.swing.JRadioButton rdoBoth;
    private javax.swing.JRadioButton rdoGABA;
    private javax.swing.JRadioButton rdoGlu;
    private javax.swing.JRadioButton rdoShfGABA;
    private javax.swing.JRadioButton rdoShfGlu;
    private javax.swing.ButtonGroup shuffledGrp;
    private javax.swing.JScrollPane weightPane;
    private transient javax.swing.JTable weightTable;
    // End of variables declaration//GEN-END:variables
    private String pathToFile;
    private int div = 0;
    private int type = 0;
    private ClusterWeightResult[] weight;
}
